package com.shop.cereshop.agent.service.FaceCheckService.impl;

import com.aliyun.cloudauth20190307.models.Id3MetaVerifyResponse;
import com.aliyun.cloudauth20190307.models.Id3MetaVerifyResponseBody;
import com.aliyun.credentials.Client;
import com.aliyun.credentials.models.Config;
import com.aliyun.tea.TeaException;
import com.shop.cereshop.commons.domain.user.faceCheck.FaceCheckModelTwo;
import com.shop.cereshop.commons.domain.user.faceCheck.respond.FaceCheckRespondTwo;
import com.shop.cereshop.agent.service.FaceCheckService.FaceCheckServiceTwo;
import org.springframework.stereotype.Service;

@Service("FaceCheckServiceTwoImpl")
public class FaceCheckServiceTwoImpl implements FaceCheckServiceTwo {



    public static com.aliyun.cloudauth20190307.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
        Config credentialConfig = new Config();
        credentialConfig.setType("access_key");
        // 注意：生产环境中不建议硬编码密钥
        credentialConfig.setAccessKeyId("LTAI5tEVtw9PvGaWNBp1HFAN");
        credentialConfig.setAccessKeySecret("96hB5UqenJf1tuGlLVXPHFephSQzAe");
        // 必填参数，此处以从环境变量中获取AccessKey Secret为例;
        Client credentialClient = new Client(credentialConfig);

        // 若使用云产品 V2.0 SDK 时，使用com.aliyun.teaopenapi.models.Config传递credential
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.setCredential(credentialClient);
        // Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
        config.setEndpoint("cloudauth.aliyuncs.com");
        return new com.aliyun.cloudauth20190307.Client(config);
    }


    @Override
    public FaceCheckRespondTwo faceCheckTwo(FaceCheckModelTwo model) throws Exception {
        com.aliyun.cloudauth20190307.Client client = FaceCheckServiceTwoImpl.createClient();
        com.aliyun.cloudauth20190307.models.Id3MetaVerifyRequest id3MetaVerifyRequest = new com.aliyun.cloudauth20190307.models.Id3MetaVerifyRequest()
                .setParamType(model.getParamType())
                .setIdentifyNum(model.getIdCardNumber())
                .setUserName(model.getUserName())
                .setCrop(model.getCrop());
        if (model.getFaceUrl() != null) {
            id3MetaVerifyRequest.setFaceUrl(model.getFaceUrl());
        } else {
            id3MetaVerifyRequest.setFaceFile(model.getFaceFile());
        }
        FaceCheckRespondTwo faceCheckRespondTwo=null;
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            Id3MetaVerifyResponse response = client.id3MetaVerifyWithOptions(id3MetaVerifyRequest, runtime);
            Id3MetaVerifyResponseBody body = response.getBody();
            faceCheckRespondTwo = new FaceCheckRespondTwo();
            faceCheckRespondTwo.setRequestId(body.requestId);
            faceCheckRespondTwo.setCode(body.code);
            faceCheckRespondTwo.setMessage(body.message);
            FaceCheckRespondTwo.ResultObject resultObject = new FaceCheckRespondTwo.ResultObject();
            resultObject.setFaceDetail(body.resultObject.faceDetail);
            resultObject.setBizCode(body.resultObject.bizCode);
            faceCheckRespondTwo.setResultObject(resultObject);

        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
      return faceCheckRespondTwo;
    }

}




